/**
 * 作者:hanbo
 * 日期:2018/12/69:27
 * 描述:多边形
 */
import {Geometry} from "./Geometry";
import {BoundingBox} from "./BoundingBox";
import {PolygonStyle} from "./style/PolygonStyle";
import {Point} from "./Point";

export class Polygon extends Geometry {
    /**
     * 构造器
     * @param array {Array} 二维点数组[[x1,y1],[x2,y2]]
     */
    constructor(array) {
        super("polygon", BoundingBox.getPolylineBox(array));
        this.array = array;
    }

    /**
     * 设置样式
     * @param polygonStyle {PolygonStyle} 面样式
     */
    setStyle(polygonStyle) {
        this.style = polygonStyle
    }

    /**
     * 获取多边形样式
     * @returns {PolygonStyle} 若无样式，范围默认样式
     */
    getStyle() {
        if (this.style) {
            return this.style;
        } else {
            return new PolygonStyle()
        }
    }

    /**
     * 批量计算多边形内部点数量
     * @param polygons
     */
    static getPointAmount(polygons) {
        let amount = 0;
        for (let i = 0; i < polygons.length; i++) {
            amount += polygons[i].array.length;
        }
        return amount;
    }

    /**
     * 生成随机三角形
     * @param pointAmount
     */
    static randomPolygon(){
        const pointAmount=3;
        let path=[];
        for (let i = 0; i < pointAmount; i++) {
            let point=Point.randomPoint();
            path.push([point.x,point.y])
        }
        return new Polygon(path)
    }

}
